//
//  SelectPersonViewController.m
//  MyProject
//
//  Created by Pei on 16/7/31.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "SelectPersonViewController.h"
#import "UserInfo.h"
@interface SelectPersonViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_dataArray;
    UITableView *_myTable;
    UITableView *_secondTable;
    
    NSArray *_secondArray;
    
    NSIndexPath *_lastIndexPath;
    
    NSString *_currentStr;
    
    NSDictionary *superDic;
    
    //NSMutableArray  *fruitFilterRecordArray;    // 定义全局变量，存储多选页面选中的选项
}

@end

@implementation SelectPersonViewController

- (void)viewDidLoad {
    self.navTitle = @"检查人选择";
    [super viewDidLoad];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    //[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    
    //fruitFilterRecordArray = [[NSMutableArray alloc] initWithCapacity:3];
    
    //_lastIndexPath = nil;
    
    [self initTableView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(kDeviceWidth/2.0f, 0.0f, 1.0f, kDeviceHeight-NAV_HEIGHT);
    lineView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:lineView];
    
    [self initSecondTableView];
    
    [self getData];
}

- (void)rightBtnAcion{
    NSMutableArray *nnnn = [[NSMutableArray alloc] initWithCapacity:3];
    [nnnn removeAllObjects];
    for (NSDictionary *ttttt in _secondArray) {
        if ([[ttttt valueForKey:@"isSelect"] intValue] == 1) {
            [nnnn addObject:ttttt];
        }
    }
    
    if (nnnn != nil && nnnn.count > 0) {
        if ([self.delegate respondsToSelector:@selector(passPersonData:super:)]) {
            [self.delegate passPersonData:nnnn super:superDic];
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initTableView
{
    _myTable = [[UITableView alloc] init];
    _myTable.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth/2.0f-0.5f, kDeviceHeight-NAV_HEIGHT);
    _myTable.delegate = self;
    _myTable.dataSource = self;
    _myTable.tag = 116;
    [self.view addSubview:_myTable];
    
    _myTable.tableFooterView = [UIView new];
}

- (void)initSecondTableView{
    //_secondTable
    _secondTable = [[UITableView alloc] init];
    _secondTable.frame = CGRectMake(kDeviceWidth/2.0f, 0.0f, kDeviceWidth/2.0f, kDeviceHeight-NAV_HEIGHT);
    _secondTable.delegate = self;
    _secondTable.dataSource = self;
    _secondTable.tag = 119;
    [self.view addSubview:_secondTable];
    
    _secondTable.tableFooterView = [UIView new];
}

- (void)getData{
    [[YMNetLib getInstance] connectWithAction:Get_Company_List method:@"GET" withContent:[NSString stringWithFormat:@"UserID=%@", [[UserInfo getInstance] getUserID]] withBody:nil withDelegate:self];
    _currentStr = Get_Company_List;
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([_currentStr isEqualToString:Get_Company_List]) {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
        {
            _dataArray = [body valueForKey:@"OrgList"];
//            NSMutableArray *tttt = [[NSMutableArray alloc] initWithCapacity:3];
//            [tttt removeAllObjects];
//            for (NSDictionary *dddd in _dataArray) {
//                if ([[dddd valueForKey:@"OrgLevel"] intValue] == 1) {
//                    [tttt addObject:dddd];
//                }
//                
//            }
//            _dataArray = tttt;
            [_myTable reloadData];
            
            if (_dataArray != nil && _dataArray.count >0) {
                NSDictionary *aaaaa = [_dataArray objectAtIndex:0];
                superDic = aaaaa;
                //[self getProjectListData:[aaaaa objectForKey:@"OrgCode"]];
                [self getPersonList:aaaaa];
            }
        }
    }else{
        _secondArray = [body objectForKey:@"UserList"];
        
        NSMutableArray *tttt = [[NSMutableArray alloc] initWithCapacity:3];
        [tttt removeAllObjects];
        for (NSDictionary *dddd in _secondArray) {
            NSMutableDictionary *yyyy = [[NSMutableDictionary alloc] initWithDictionary:dddd];
            [yyyy setValue:@"0" forKey:@"isSelect"];
            [tttt addObject:yyyy];
        }
        _secondArray = tttt;
        
        [_secondTable reloadData];
    }
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag == 116) {
        return _dataArray.count;
    }else{
        return _secondArray.count;
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 116) {
        static NSString *identifier = @"first_cell";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.textColor = kRGB(163, 163, 163);
        cell.textLabel.font = kAppFont(Home_Small_Size+2);
        //cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *bgColorView = [[UIView alloc] init];
        bgColorView.backgroundColor = kRGB(226, 226, 226);
        bgColorView.layer.masksToBounds = YES;
        cell.selectedBackgroundView = bgColorView;
        
        NSDictionary *tempDic = [_dataArray objectAtIndex:indexPath.row];
        
        if (indexPath.row == 0) {
            [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }

        
        cell.textLabel.text = [tempDic valueForKey:@"OrgName"];
        return cell;
    }
    else{
        static NSString *identifier = @"cell_house_History";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.textColor = kRGB(163, 163, 163);
        cell.textLabel.font = kAppFont(Home_Small_Size+2);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSDictionary *tempDic = [_secondArray objectAtIndex:indexPath.row];
        cell.textLabel.text = [tempDic valueForKey:@"UserName"];
        
        if ([[tempDic valueForKey:@"isSelect"] intValue] == 1) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;//勾标记
        }else{
            cell.accessoryType = UITableViewCellAccessoryNone;//不标记
        }
        
        //cell.textLabel.text = @"6666666";
        
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54.0f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 116) {
        NSDictionary *tempDic = [_dataArray objectAtIndex:indexPath.row];
        superDic = tempDic;
        [self getPersonList:tempDic];
    }else{
        
        NSInteger newRow = [indexPath row];
        NSInteger oldRow = -1;
        if (_lastIndexPath != nil)
        {
            oldRow = [_lastIndexPath row];
        }
        
        
        if (newRow != oldRow)
        {
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                        indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath];
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            
            _lastIndexPath = indexPath;
        }
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        //NSDictionary *tempDic = [_dataArray objectAtIndex:indexPath.row];
        NSDictionary *tempDic = [_secondArray objectAtIndex:indexPath.row];
        
        if (tempDic != nil && tempDic.count > 0) {
            if ([self.delegate respondsToSelector:@selector(passPersonData:super:)]) {
                [self.delegate passPersonData:tempDic super:superDic];
            }

        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
       /*
        NSDictionary *tempDic = [_secondArray objectAtIndex:indexPath.row];
        
        if ([[tempDic valueForKey:@"isSelect"] intValue] == 0) {
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                        indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            
            NSMutableDictionary *aaaa = [[NSMutableDictionary alloc] initWithDictionary:tempDic];
            [aaaa setValue:@"1" forKey:@"isSelect"];
            
            NSMutableArray *tttttt = [[NSMutableArray alloc] initWithArray:_secondArray];
            for (int i = 0; i < _secondArray.count; i ++) {
                NSDictionary *rrrr = [_secondArray objectAtIndex:i];
                if ([[rrrr valueForKey:@"UserID"] isEqualToString:[aaaa valueForKey:@"UserID"]]) {
                    [tttttt replaceObjectAtIndex:i withObject:aaaa];
                }
            }
            
            _secondArray = tttttt;
            
        }else{
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                        indexPath];
            newCell.accessoryType = UITableViewCellAccessoryNone;
            
            NSMutableDictionary *aaaa = [[NSMutableDictionary alloc] initWithDictionary:tempDic];
            [aaaa setValue:@"0" forKey:@"isSelect"];
            
            NSMutableArray *tttttt = [[NSMutableArray alloc] initWithArray:_secondArray];
            for (int i = 0; i < _secondArray.count; i ++) {
                NSDictionary *rrrr = [_secondArray objectAtIndex:i];
                if ([[rrrr valueForKey:@"UserID"] isEqualToString:[aaaa valueForKey:@"UserID"]]) {
                    [tttttt replaceObjectAtIndex:i withObject:aaaa];
                }
            }
            
            _secondArray = tttttt;
        }
        [_secondTable reloadData];
    }
        */
    
}

- (void)getPersonList:(NSDictionary*)data{
    //Get_UserListByKeyWord
    [[YMNetLib getInstance] connectWithAction:Get_UserListByKeyWord method:@"GET" withContent:[NSString stringWithFormat:@"keyword=@""&UserRole=Inspect&DeptCode=%@", [data valueForKey:@"OrgCode"]] withBody:nil withDelegate:self];
    _currentStr = Get_UserListByKeyWord;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
